package com.blackteachan.server.controller;

import com.blackteachan.server.annotation.LogAnnotation;
import com.blackteachan.server.dto.UserDto;
import com.blackteachan.server.response.ServerResponse;
import com.blackteachan.server.response.ServerResponseUtil;
import com.blackteachan.server.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Demo
 * @author blackteachan
 * 创建日期 2020-07-22 10:58
 */
@RestController
@RequestMapping("demo")
public class DemoController {

    @Autowired
    private UserService userService;

    @LogAnnotation
    @ApiOperation("测试接口")
    @Transactional(rollbackFor = Exception.class)
    @GetMapping("test")
    public ServerResponse test(){
        try {
            //插入两条数据然后抛出异常测试回滚
            UserDto userDto = new UserDto();
            userDto.setUsername("陈浩" + Math.random());
            userDto.setPassword("123456");
            userDto.setSalt("123456");
            userService.saveUser(userDto);
            userDto.setUsername("陈红茶" + Math.random());
            userService.saveUser(userDto);

            Object obj = null;
            //抛出异常
            Assert.notNull(obj, "😁rz");
            return ServerResponseUtil.success();
        }catch (Exception e){
            e.printStackTrace();
            throw e;
        }
    }
}
